package cn.seecoder.ai.controller;

import cn.seecoder.ai.model.vo.FileInfoVO;
import cn.seecoder.ai.model.vo.ResultVO;
import cn.seecoder.ai.service.FileFieldService;
import cn.seecoder.ai.service.FileInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author fanyanpeng
 * @date 2023/4/11 20:11
 */
@Slf4j
@RestController
@RequestMapping("${apiPrefix}/files")
public class FileInfoController {

    @Autowired
    FileInfoService fileInfoService;

    @Autowired
    FileFieldService fileFieldService;

    @ApiOperation("文件数据导入")
    @PostMapping("/file")
    public ResultVO<FileInfoVO> addFile(@RequestParam String fileName,
                                        @RequestParam Integer userId,
                                        @RequestParam MultipartFile file){
        FileInfoVO fileInfoVO = fileInfoService.addFile(fileName,userId,file);
        fileFieldService.analysisField(userId,fileInfoVO.getId());
        fileInfoVO = fileInfoService.getFileInfo(userId,fileInfoVO.getId());
        return ResultVO.buildSuccess(fileInfoVO);
    }

    @ApiOperation("查看文件分析信息")
    @GetMapping("/{fileInfoId}")
    public ResultVO<FileInfoVO> getFileInfo(@RequestParam Integer userId,
                                            @PathVariable Integer fileInfoId){

        FileInfoVO fileInfoVO = fileInfoService.getFileInfo(userId,fileInfoId);
        //若文件未被分析，进行分析！
        if(fileInfoVO.getFileFields().isEmpty()){
            fileFieldService.analysisField(userId,fileInfoId);
        }
        fileInfoVO = fileInfoService.getFileInfo(userId,fileInfoId);
        return ResultVO.buildSuccess(fileInfoVO);

    }

    @ApiOperation("文件描述")
    @GetMapping("/describe/{fileInfoId}")
    public ResultVO<FileInfoVO> describeFileInfo(@RequestParam Integer userId,
                                            @PathVariable Integer fileInfoId){

        fileFieldService.analysisField(userId,fileInfoId);

        FileInfoVO fileInfoVO = fileInfoService.getFileInfo(userId,fileInfoId);
        return ResultVO.buildSuccess(fileInfoVO);
    }

    @ApiOperation("查看我的文件列表")
    @GetMapping("")
    public ResultVO<List<FileInfoVO>> getFileInfoListByUserId(@RequestParam Integer userId){
        return ResultVO.buildSuccess(fileInfoService.getFileInfoListByUserId(userId));
    }

    @ApiOperation("表数据导入")
    @PostMapping("/table")
    public ResultVO<FileInfoVO> addTable(@RequestParam String url,
                                         @RequestParam String user,
                                         @RequestParam String password,
                                         @RequestParam String tableName,
                                         @RequestParam String fileName,
                                         @RequestParam Integer userId){
        FileInfoVO fileInfoVO = fileInfoService.addTable(url, user, password, tableName, fileName, userId);
        fileFieldService.analysisField(userId,fileInfoVO.getId());
        fileInfoVO = fileInfoService.getFileInfo(userId,fileInfoVO.getId());
        return ResultVO.buildSuccess(fileInfoVO);
    }
}
